package com.transcargo.rating;

/**
 * Date: 24.04.2011
 * Time: 16:17:40
 */

import com.transcargo.db.db;
import com.transcargo.db.dbException;

import java.util.ArrayList;
import java.util.Iterator;

public class Debt {
    private int Id = 0;
    private int Debitor = 0;
    private int Creditor = 0;
    private String date = "";
    private String amount = "";
    private String comment = "";
    private Comment comments[] = null;
    private String debitor_name = "";
    public Debt(int id, db dbase) {
        Id = id;
        try {
            ArrayList<String[]> list = dbase.execute("SELECT debitor, creditor, date, amount, comment FROM debts WHERE id=" + id);
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    String[] ctr = iter.next();
                    Debitor = Integer.parseInt(ctr[0]);
                    Creditor = Integer.parseInt(ctr[1]);
                    date = ctr[2];
                    amount = ctr[3];
                    comment = ctr[4];
                    list = dbase.execute("SELECT id FROM comments WHERE debt_id=" + id);
                    iter = list.iterator();
                    comments = new Comment[list.size()];
                    int i = 0;
                    while (iter.hasNext()) {
                        ctr = iter.next();
                        comments[i++] = new Comment(Integer.parseInt(ctr[0]), id, dbase);
                    }
                } else System.out.println("Debt exception! Unable to find debt id " + id);
            } else System.out.println("Debt exception! Unable to find debt id " + id);
        } catch (dbException e) {
            System.out.println("Debt exception! Unable to find debt id " + id);
        }
    }
    public int getId() {
        return Id;
    }
    public int getDebitor() {
        return Debitor;
    }
    public int getCreditor() {
        return Creditor;
    }
    public String getDate() {
        return date;
    }
    public String getAmount() {
        return amount;
    }
    public String getComment() {
        return comment;
    }
    public Comment[] getComments() {
        return comments;
    }
}
